#!/usr/bin/env python

"""
#	A simple IRC Bot, that logs conversation in an IRC Channel
#
#	(C) 2008, Pranav Prakash
#
#	Email: pranny@gmail.com
#
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""

import string, socket, re
import pars

class Bot(object):
	"A Simple Bot class"
	
	"""This is the bot class, and the actual bots will be instances of
	this class"""
	
	def __init__(self, HOST, PORT, NICK, REALNAME, IDENTITY):
		self.host = HOST
		self.port = PORT
		self.nick = NICK
		self.realname = REALNAME
		self.identity = IDENTITY
		self.connected = 0 # disconnected by default
		self.sock = socket.socket()
		self.join = 0
		self.p = pars.Parser()			# Parser
		#self.a = act.Action()			# Action to be taken
		
	
	def connect(self, HOST = 'irc.freenode.net', PORT = 6667):
		loop = 1
		if self.connected == 1:
			print "Disconnecting from %s:%s" %(self.host, self.port)
			self.connected = 0
			self.host = HOST
			self.port = PORT
			self.sock.close()
		else:
			print "Connecting to %s:%s" %(self.host, self.port)
			self.sock.connect((self.host, self.port))
			self.sock.send('NICK ' + self.nick + '\n')
			self.sock.send('USER ' + self.identity + ' ' + self.host + ' blah :' + self.realname+'\n')
			while loop == 1:
				t = self.sock.recv(512)
				print t
				if t.find('PRIVMSG') != -1:
					self.join =  1		# ready to Join a channel
					return
				if len(t) < 1:
					loop = 0
					self.join = 0				# not ready to join a channel			
			
	def joinChannel(self, CHANNEL):
		self.channel = CHANNEL
		loop = 1
		print self.join
		if self.join == 1:
			self.sock.send('JOIN '+self.channel+'\n')
			while loop ==1:
				t = self.sock.recv(512)
				logIt(t)
				if t.find("PING :") != -1:
					"""Just recieved a PING request from the sever
					Hence, bot responds back by PONG message"""
					self.sock.send("PONG :" + re.split(":", t)[1]+"\n")
					logIt("PONG :" + re.split(":", t)[1])
					
				else:
					self.p.parse(t)
					if string.find(self.p.getSender(),'pranny') != -1 and string.find(self.p.getMessage(),'EXIT') != -1:
						self.sock.send("QUIT :Out For chips, MicroChips")
						loop = 0
						break
					try:
						m = re.search('(fuck|shit|bastard|asshole|suck)', t)
						print m.group(0)
						self.sock.send("PRIVMSG " + self.channel+ " :"+ self.p.getSender() +", that was quite rude. I hope it is not repeated\n")
					except:
						pass
					
				
		else:
			print "Not ready to join"

	def disconnect(self):
		if self.connected == 1:
			self.sock.close()

def logIt(text):
	f = open('logfile','a')
	f.write(text)
	f.close()
		
				
if __name__ == '__main__':
	piku = Bot('irc.freenode.net', 6667, 'piku_b02', 'piku', 'piku_b02')
	piku.connect()
	piku.joinChannel('#orkut_linux')
